package kiven.jee.pagination;

import java.util.List;

import kiven.jee.utils.common.Constants;

/**
 * 动态分页实现.每次查询返回一页记录的情况下使用.
 * 
 * @author kiven
 * @date 2013-03-04
 *
 * @param <E>
 */
public class QuickPageContext<E> implements IPageContext<E> {
	// Page中的记录list
	private List<E> items;
	private int totalCount;
	private int pageSize;
	
	/**
	 * 构造函数
	 * 
	 * @param totalCount 总记录数
	 * @param pageSize 每页显示记录数目
	 * @param items 每页显示记录条数
	 */
	public QuickPageContext(int totalCount,int pageSize,List<E> items) {
		this.totalCount = totalCount;
		this.pageSize = (pageSize == 0 ? Constants.DEFAULT_PAGE_SIZE : pageSize);
		if (items != null)
			this.items = items;
	}

	@Override
	public int getPageCount() {
		int div = totalCount / pageSize;
		int result = (totalCount % pageSize ==0) ? div : div + 1;
		return result;
	}

	@Override
	public Page<E> getPage(int index) {
		Page<E> page = new Page<E>();
		page.setContext(this);
		int index2 = index > getPageCount() ? 1 : index;
		page.setHasNext(index2 < getPageCount());
		page.setHasPre(index2 > 2);
		page.setItems(this.items);
		return null;
	}

	@Override
	public int getPageSize() {
		return this.pageSize;
	}

	@Override
	public int getTotal() {
		return this.totalCount;
	}

}
